package com.tiger.project.service.impl;
import java.util.List;

import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.*;
import com.tiger.project.mapper.ContractMapper;
import com.tiger.project.mapper.ProjectAcceptanceEnclosureMapper;
import com.tiger.project.mapper.ProjectAcceptanceLinkmanMapper;
import com.tiger.project.mapper.ProjectAcceptanceMapper;
import com.tiger.project.service.IProjectAcceptanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【项目验收】Service业务层处理
 *
 * @author standard
 * @date 2022-09-19
 */
@Service
public class ProjectAcceptanceServiceImpl implements IProjectAcceptanceService
{
    @Autowired
    private ProjectAcceptanceMapper projectAcceptanceMapper;
    @Autowired
    private ProjectAcceptanceLinkmanMapper linkmanMapper;
    @Autowired
    private ProjectAcceptanceEnclosureMapper enclosureMapper;
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询【项目验收】
     *
     * @param id 【项目验收】主键
     * @return 【项目验收】
     */
    @Override
    public ProjectAcceptance selectProjectAcceptanceById(Long id) {
        ProjectAcceptance projectAcceptance = projectAcceptanceMapper.selectProjectAcceptanceById(id);

        //附件
        ProjectAcceptanceEnclosure enclosure = new ProjectAcceptanceEnclosure();
        enclosure.setAcceptanceId(id);
        List<ProjectAcceptanceEnclosure> list = enclosureMapper.selectProjectAcceptanceEnclosureList(enclosure);
        if (list !=null && list.size() > 0){
            projectAcceptance.getEnclosureList().addAll(list);
        }
        //联系人
        ProjectAcceptanceLinkman linkman = new ProjectAcceptanceLinkman();
        linkman.setProjectAcceptanceId(id);
        List<ProjectAcceptanceLinkman> linkmanList = linkmanMapper.selectProjectAcceptanceLinkmanList(linkman);
        if (linkmanList !=null && linkmanList.size() > 0){
            projectAcceptance.getLinkmanList().addAll(linkmanList);
        }
        //合同
        Contract contract = contractMapper.selectContractById(projectAcceptance.getContractId());
        projectAcceptance.getContractList().add(contract);

        return projectAcceptance;
    }

    /**
     * 查询【项目验收】列表
     *
     * @param projectAcceptance 【项目验收】
     * @return 【项目验收】
     */
    @Override
    public List<ProjectAcceptance> selectProjectAcceptanceList(ProjectAcceptance projectAcceptance) {
        return projectAcceptanceMapper.selectProjectAcceptanceList(projectAcceptance);
    }

    /**
     * 新增【项目验收】
     *
     * @param projectAcceptance 【项目验收】
     * @return 结果
     */
    @Override
    public int insertProjectAcceptance(ProjectAcceptance projectAcceptance) {
        projectAcceptance.setCreatedAt(DateUtils.getNowDate());
        projectAcceptance.setIsDeleted(0L);
        projectAcceptance.setStatus(2L); //未提交
        int i = projectAcceptanceMapper.updateProjectAcceptance(projectAcceptance);
        change(projectAcceptance);
        return i;
    }

    /**
     * 修改【项目验收】
     *
     * @param projectAcceptance 【项目验收】
     * @return 结果
     */
    @Override
    public int updateProjectAcceptance(ProjectAcceptance projectAcceptance) {
        change(projectAcceptance);
        projectAcceptance.setUpdatedAt(DateUtils.getNowDate());
        projectAcceptance.setStatus(2L); //未提交
        return projectAcceptanceMapper.updateProjectAcceptance(projectAcceptance);
    }

    /**
     * 批量删除【项目验收】
     *
     * @param ids 需要删除的【项目验收】主键
     * @return 结果
     */
    @Override
    public int deleteProjectAcceptanceByIds(Long[] ids) {
        return projectAcceptanceMapper.deleteProjectAcceptanceByIds(ids);
    }

    /**
     * 删除【项目验收】信息
     *
     * @param id 【项目验收】主键
     * @return 结果
     */
    @Override
    public int deleteProjectAcceptanceById(Long id) {
        enclosureMapper.deleteProjectAcceptanceEnclosureByProjectId(id);
        linkmanMapper.deleteProjectAcceptanceLinkmanByProjectId(id);
        ProjectAcceptance projectAcceptance = selectProjectAcceptanceById(id);
        projectAcceptance.setAcceptedAt(null);
        projectAcceptance.setProjectManager(null);
        projectAcceptance.setStartAt(null);
        projectAcceptance.setEndAt(null);
        projectAcceptance.setResult(null);
        projectAcceptance.setStatus(1L);
        return projectAcceptanceMapper.updateProjectAcceptance(projectAcceptance);
    }

    //审核回调
    @Override
    public int callBack(Long id, Long status,String oaFile) {
        ProjectAcceptance projectAcceptance = projectAcceptanceMapper.selectProjectAcceptanceById(id);
        projectAcceptance.setUpdatedAt(DateUtils.getNowDate());
        projectAcceptance.setStatus(status);
        projectAcceptance.setOaFile(oaFile);
        return projectAcceptanceMapper.updateProjectAcceptance(projectAcceptance);
    }

    private void change(ProjectAcceptance projectAcceptance){
        //附件
        enclosureMapper.deleteProjectAcceptanceEnclosureByProjectId(projectAcceptance.getId());
        if (StringUtils.isNotEmpty(projectAcceptance.getEnclosureList())){
            projectAcceptance.getEnclosureList().forEach(item ->{
                item.setCreatedAt(DateUtils.getNowDate());
                item.setState("1");//上传成功
                item.setAcceptanceId(projectAcceptance.getId());
                enclosureMapper.insertProjectAcceptanceEnclosure(item);
            });
        }
        //联系人
        linkmanMapper.deleteProjectAcceptanceLinkmanByProjectId(projectAcceptance.getId());
        if (StringUtils.isNotEmpty(projectAcceptance.getLinkmanList())){
            projectAcceptance.getLinkmanList().forEach(linkman ->{
                linkman.setProjectAcceptanceId(projectAcceptance.getId());
                linkmanMapper.insertProjectAcceptanceLinkman(linkman);
            });
        }
    }


}
